package DP;

import java.util.Scanner;

/**
 * @author dx
 * @version 1.0
 * @date 2022/5/16 17:25
 * @description: 力扣14 最长公共前缀
 */
public class Longestprefix {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        String [] strs = sr.nextLine().split(" ");
        String x =new Longestprefix_Solution().longestCommonPrefix1(strs);
        String x2 =new Longestprefix_Solution().longestCommonPrefix2(strs);
        System.out.println(x);
        System.out.println(x2);
    }
}

class Longestprefix_Solution{
    //横向
    public String longestCommonPrefix1(String[] strs) {
        if(strs.length == 0 || strs == null){
            return "";
        }
        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {
            prefix = longconmmon(prefix, strs[i]);
        }
        return prefix;
    }
    public String longconmmon(String s1,String s2){
        int len = Math.min(s1.length(),s2.length());
        int index = 0;
        while(index<len){
            if(s1.charAt(index) != s2.charAt(index)){
                break;
            }
            index++;
        }
        return s1.substring(0,index);
    }
    //纵向
    public String longestCommonPrefix2(String[] strs){
        String ans = "";
        if(strs.length == 0 || strs == null){
            return ans;
        }

        for(int i = 0;i < strs.length;i++){
            char c = strs[0].charAt(i);
            for(int j = 1;j<strs.length;j++){
                if(longCommon(strs[j],c,i)){
                    ans = strs[0].substring(0,i);
                }
            }
        }
        return ans;


    }
    public boolean longCommon(String s,char c,int index){
        if(s.charAt(index) != c || index == s.length()){
            return true;
        }
        return false;
    }
}
